package top.songxs.yygh.service.order.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.songxs.yygh.common.result.R;
import top.songxs.yygh.common.utils.JwtHelper;
import top.songxs.yygh.enums.OrderStatusEnum;
import top.songxs.yygh.model.order.OrderInfo;
import top.songxs.yygh.service.order.service.OrderInfoService;
import top.songxs.yygh.service.order.service.OrderService;
import top.songxs.yygh.vo.order.OrderCountQueryVo;
import top.songxs.yygh.vo.order.OrderQueryVo;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "订单服务")
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderApiController {

    @Autowired
    OrderService orderService;

    @Autowired
    OrderInfoService orderInfoService;

    @ApiOperation(value = "创建订单")
    @PostMapping("/auth/submitOrder/{scheduleId}/{patientId}")
    public R createOrder(@PathVariable String scheduleId, @PathVariable Long patientId) {
        Map<String,Object> map = new HashMap<>();

        OrderInfo orderInfo = orderService.getByScheduleIdAndPatientId(scheduleId, patientId);
        if (orderInfo!=null){
            map.put("orderMessage",-1);
            map.put("orderId",orderInfo.getId());
            return R.ok().data(map);
        }
        Long orderId = orderService.saveOrder(scheduleId, patientId);

        return R.ok().data("orderId", orderId);
    }

    @GetMapping("/auth/getOrder/{orderId}")
    public R getOrder(@PathVariable String orderId) {
        OrderInfo orderInfo = orderInfoService.getOrderInfo(orderId);
        return R.ok().data("orderInfo", orderInfo);
    }

    @GetMapping("/auth/{pageNum}/{pageSize}")
    public R getPageList(@PathVariable Long pageNum, @PathVariable Long pageSize, OrderQueryVo orderQueryVo, HttpServletRequest request) {
        //从请求头解析令牌
        Long userId = JwtHelper.getUserId(request.getHeader("token"));
        orderQueryVo.setUserId(userId);


        Page<OrderInfo> pageModel = orderInfoService.getpageList(pageNum, pageSize, orderQueryVo);
        return R.ok().data("pageModel",pageModel);
    }

    @ApiOperation(value = "获取订单状态列表")
    @GetMapping("auth/getStatusList")
    public R getStatusList() {
        return R.ok().data("statusList", OrderStatusEnum.getStatusList());
    }

    @ApiOperation(value = "取消预约")
    @GetMapping("auth/cancelOrder/{orderId}")
    public R cancelOrder(@PathVariable("orderId") Long orderId) {
        Boolean result = orderService.cancelOrder(orderId);
        return R.ok().data("flag",result);
    }

    @PostMapping("/getCountMap")
    public Map<String,Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo) {

        return orderService.getCountMap(orderCountQueryVo);
    }
}
